#ifndef HTTP_CLIENT_H
#define HTTP_CLIENT_H

#include <string>
#include <vector>
#include <map>
#include "esp_http_client.h"
#include "esp_err.h"
#include "http.h" // Include the interface
#include "freertos/FreeRTOS.h" // For AsyncTaskParams if defined here
#include "freertos/task.h"     // For AsyncTaskParams if defined here

// Forward declaration for AsyncTaskParams if it's complex and defined in .cpp
// struct AsyncTaskParams; 

class HTTPClient : public HttpInterface {
public:
    HTTPClient();
    ~HTTPClient() override;

    // --- Configuration Methods (Implementations from HttpInterface) ---
    void setBaseUrl(const std::string& base_url) override;
    void setDefaultHeader(const std::string& name, const std::string& value) override;
    void removeDefaultHeader(const std::string& name) override;
    void clearDefaultHeaders() override;
    void setTimeout(int timeout_ms) override;

    // --- Asynchronous HTTP Request Methods (Implementations from HttpInterface) ---
    void get(
        const std::string& path,
        HttpCallback callback,
        const std::map<std::string, std::string>& query_params = {},
        const std::map<std::string, std::string>& custom_headers = {}
    ) override;

    void post(
        const std::string& path,
        const std::string& body,
        HttpCallback callback,
        const std::string& content_type = "",
        const std::map<std::string, std::string>& custom_headers = {}
    ) override;

    void put(
        const std::string& path,
        const std::string& body,
        HttpCallback callback,
        const std::string& content_type = "",
        const std::map<std::string, std::string>& custom_headers = {}
    ) override;

    void del(
        const std::string& path,
        HttpCallback callback,
        const std::map<std::string, std::string>& custom_headers = {}
    ) override;

    void patch(
        const std::string& path,
        const std::string& body,
        HttpCallback callback,
        const std::string& content_type = "",
        const std::map<std::string, std::string>& custom_headers = {}
    ) override;

    // --- Synchronous HTTP Request Methods (Implementations from HttpInterface) ---
    HttpResponse getSync(
        const std::string& path,
        const std::map<std::string, std::string>& query_params = {},
        const std::map<std::string, std::string>& custom_headers = {}
    ) override;

    HttpResponse postSync(
        const std::string& path,
        const std::string& body,
        const std::string& content_type = "",
        const std::map<std::string, std::string>& custom_headers = {}
    ) override;

    HttpResponse putSync(
        const std::string& path,
        const std::string& body,
        const std::string& content_type = "",
        const std::map<std::string, std::string>& custom_headers = {}
    ) override;

    HttpResponse delSync(
        const std::string& path,
        const std::map<std::string, std::string>& custom_headers = {}
    ) override;

    HttpResponse patchSync(
        const std::string& path,
        const std::string& body,
        const std::string& content_type = "",
        const std::map<std::string, std::string>& custom_headers = {}
    ) override;

    // Helper to get response body string
    std::string getString();
    // Helper to get response headers map
    const std::map<std::string, std::string>& getResponseHeaders() const;

// Make these accessible to the static event handler and async task function
// by making them public or providing controlled access if they were private.
// For simplicity in this context, they are accessed via the 'this' pointer passed in user_data.
// If they were private, http_event_handler would need to be a friend or have public setters.
// Since HttpInterface declares base_url_ etc. as protected, they are accessible.
// public: // Temporarily making these public for the static handler, or use friend
    std::vector<char> response_data_buffer_;  // Instance-specific response data buffer
    std::map<std::string, std::string> response_headers_;  // Stores response headers

private:
    // Removed: client_handle_, current_url_, is_initialized_
    // Inherited from HttpInterface: base_url_, default_headers_, timeout_ms_
    
    // Static event handler for ESP HTTP client
    static esp_err_t http_event_handler(esp_http_client_event_t *evt);
    
    // Static task function for asynchronous requests
    static void http_async_task_function(void* pvParameters);

    // Helper to construct full URL
    std::string build_full_url(const std::string& path, const std::map<std::string, std::string>& query_params);
    
    // Helper to configure and perform a synchronous request
    HttpResponse perform_sync_request(
        const std::string& path,
        esp_http_client_method_t method,
        const std::map<std::string, std::string>& query_params,
        const std::string& body,
        const std::string& content_type,
        const std::map<std::string, std::string>& custom_headers
    );
};

#endif // HTTP_CLIENT_H
